package com.example.combwx.mvp.presenter;

import com.blankj.utilcode.util.PhoneUtils;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.presenter.BasePresenter;
import com.example.combwx.mvp.contract.SplashContract;
import com.example.combwx.mvp.model.entity.TokenEntity;
import com.example.combwx.utils.LogUtils;
import com.example.combwx.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class SplashPresenter extends BasePresenter<SplashContract.ISplashModel, SplashContract.ISplashVIew> {

    @Inject
    public SplashPresenter(SplashContract.ISplashModel iSplashModel, SplashContract.ISplashVIew iSplashVIew) {
        super(iSplashModel, iSplashVIew);
    }

    //请求服务器token方法
    public void requestToken(){
        Map<String,Object> map = new HashMap<>();
//        String imie = PhoneUtils.getIMEI();
        map.put("imie",""+System.currentTimeMillis());
        m.requestToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onNext);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
        //请求成功逻辑校验
        TokenEntity entity = (TokenEntity) baseEntity;
        LogUtils.show("SplashPresenter->onNext");
        if (entity.status.equals("200")){
            LogUtils.show("SplashPresenter->onNext1");
            //保存token到本地SP存储中
            SpUtils.saveData("token",entity.getValues().token);
            LogUtils.show("SplashPresenter->onNext2");
            //通知activity界面跳转
            v.refresh();
        }
    }
}

